package com.yinNuo.collectWool.core;

import cn.hutool.json.JSONUtil;
import com.yinNuo.collectWool.core.constant.ResultCode;
import lombok.Data;

/**
 * @author: yinNuo
 * create at: 2021/12/6 下午7:57
 * @description: 统一API响应结果封装
 */
@Data
public class Response<T> {

        private int code;
        private String message;
        private T data;

        public static <T> Response<T> ok() {
            return R(null, ResultCode.SUCCESS, "success");
        }

        public static <T> Response<T> ok(T data) {
            return R(data, ResultCode.SUCCESS, "success");
        }

        public static <T> Response<T> failed() {
            return R(null, ResultCode.FAIL, "fail");
        }

        public static <T> Response<T> failed(String msg) {
            return R(null, ResultCode.FAIL, msg);
        }

        public static <T> Response<T> failed(T data) {
            return R(data, ResultCode.FAIL, "fail");
        }

        public static <T> Response<T> failed(T data, String msg) {
            return R(data, ResultCode.FAIL, msg);
        }

        public static <T> Response<T> failed(T data, int code, String msg) {
            return R(data, code, msg);
        }

        private static <T> Response<T> R(T data, int code, String msg) {
            Response<T> r = new Response<>();
            r.setCode(code);
            r.setData(data);
            r.setMessage(msg);
            return r;
        }


        @Override
        public String toString() {
            return JSONUtil.toJsonStr(this);
        }
    }
